using IQIGame.Onigao.Framework.Serialization;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public partial class LevelServiceBlackboard
    {
        private Dictionary<string, List<int>> pathCache => this.editorCache?.allPathToService;

        // 配置缓存，减少从硬盘中获取配置的次数
        private Dictionary<int, EditorLevelServiceConfig> configCache = new Dictionary<int, EditorLevelServiceConfig>();

        #region DrawerAPI

        #region PickPoint

        public SerializedProperty pickProperty { get; private set; }
        private List<Vector3> pickPoint = new List<Vector3>();
        public string inPicking;

        public void SwitchPick(SerializedProperty property)
        {
            if (this.pickProperty == null)
            {
                SceneView.duringSceneGui += OnSceneGUI;
            }
            if (this.pickProperty?.propertyPath != property.propertyPath)
            {
                this.pickProperty = property;
                this.inPicking = this.pickProperty.propertyPath;
                this.pickPoint.Clear();
                if (this.pickProperty.isArray)
                {
                    for (int i = 0; i < this.pickProperty.arraySize; i++)
                    {
                        var rPoint = this.pickProperty.GetArrayElementAtIndex(i).vector3Value;
                        if (rPoint != Vector3.zero)
                        {
                            this.pickPoint.Add(rPoint);
                        }
                    }
                }
                else
                {
                    if (this.pickProperty.vector3Value != Vector3.zero)
                    {
                        this.pickPoint.Add(this.pickProperty.vector3Value);
                    }
                }
            }
            else
            {
                this.StopPick();
            }
        }

        public void StopPick()
        {
            SceneView.duringSceneGui -= OnSceneGUI;
            this.pickPoint.Clear();
            this.pickProperty = null;
            this.inPicking = string.Empty;
        }

        private void OnSceneGUI(SceneView sceneView)
        {
            if (this.pickProperty == null)
            {
                return;
            }
            Event e = Event.current;
            if (e.type == EventType.MouseDown && e.button == 0)
            {
                Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);
                if (Physics.Raycast(ray, out RaycastHit hit, float.PositiveInfinity, LayerMaskConsts.Layer_Ground))
                {
                    if (this.pickProperty.isArray)
                    {
                        this.pickProperty.InsertArrayElementAtIndex(this.pickProperty.arraySize);
                        var rValueProperty = this.pickProperty.GetArrayElementAtIndex(this.pickProperty.arraySize - 1);
                        rValueProperty.vector3Value = hit.point;

                        this.pickPoint.Add(hit.point);
                    }
                    else
                    {
                        this.pickProperty.vector3Value = hit.point;
                        if (this.pickPoint.Count <= 0)
                        {
                            this.pickPoint.Add(hit.point);
                        }
                        else
                        {
                            this.pickPoint[0] = hit.point;
                        }
                    }
                    this.pickProperty.serializedObject.ApplyModifiedProperties();
                }
            }

            Handles.color = Color.red;
            for (int i = 0; i < this.pickPoint.Count; i++)
            {
                Handles.DrawSolidDisc(this.pickPoint[i], Vector3.up, 1f);
            }
            Handles.color = Color.white;
        }

        #endregion

        #region 获取已序列化后的数据

        public List<int> GetPathGroup(string assetPath)
        {
            assetPath = assetPath.Replace(LevelServiceBlackboard.BaseDirPath, "");
            if (this.pathCache != null &&this.pathCache.TryGetValue(assetPath, out var rGroup))
            {
                return rGroup;
            }
            return new List<int>();
        }

        public EditorLevelServiceConfig GetService(int nID)
        {
            if (nID == 0 || this.pathCache == null)
            {
                return null;
            }
            if (!this.configCache.TryGetValue(nID, out var rConfig))
            {
                foreach (var rItem in this.pathCache)
                {
                    var rAssetPath = LevelServiceBlackboard.BaseDirPath + rItem.Key;
                    var rGroup = rItem.Value;
                    if (rGroup.Contains(nID))
                    {
                        var rBytePath = this.GetBytesPathByAssetPath(rAssetPath);
                        var rByteBuf = new ByteBuf(File.ReadAllBytes(rBytePath));
                        var rList = rByteBuf.DeSerializeList<EditorLevelServiceConfig>();
                        for (int i = 0; i < rList.Count; i++)
                        {
                            this.configCache[rList[i].id] = rList[i];
                        }
                        break;
                    }
                }
            }
            this.configCache.TryGetValue(nID, out rConfig);
            return rConfig;
        }

        #endregion

        #endregion
    }
}